package com.example.popupmenudemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
import android.widget.PopupMenu.OnMenuItemClickListener;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnMenuItemClickListener{
	
	private final static int ONE = 1;
    private final static int TWO = 2;
    private final static int THREE = 3;

    private Button button;
    private PopupMenu popupMenu;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (Button)findViewById(R.id.button1);
		
		popupMenu = new PopupMenu(MainActivity.this, button);
		popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "item 1");
		popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, "item 2");
		popupMenu.getMenu().add(Menu.NONE, THREE, Menu.NONE, "item 3");		
		
		popupMenu.setOnMenuItemClickListener(this);
	}
	
	public void showPopupMenu(View view){
		
		popupMenu.show();
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemClick(MenuItem arg0) {
		// TODO Auto-generated method stub
		
		int key = arg0.getItemId();
		switch (key) {
		case ONE:
			Toast.makeText(this, "one", Toast.LENGTH_LONG).show();
			break;
		case TWO:
			Toast.makeText(this, "two", Toast.LENGTH_LONG).show();
			break;
			
		case THREE:
			Toast.makeText(this, "three", Toast.LENGTH_LONG).show();
			break;

		default:
			break;
		}
		return false;
	}

}
